import axios from 'axios'
import store from '../store'
import {message} from 'ant-design-vue'
import router from '../router'
const request = axios.create({
    baseURL:process.env.VUE_APP_BASE_API,//代理api
    timeout:10000
})
request.interceptors.request.use(
    config => {
      config.headers.common.Authorization = "Bearer "+store.getters.getToken
      return config;
    },
    error => {  
      return Promise.reject(error);
    }
  );
  
  request.interceptors.response.use(
    res => {
      const backData = res.data
      if(backData.status===902){
        store.dispatch('removeToken')
        router.replace(`/login?redirect=${window.location.pathname+window.location.search}`)
      }
      return backData;
    },
    error => {
      if(!error.response){
        message.error('系统繁忙，请稍后重试')
      }else{
        if(error.response.status===500){
          message.error('服务器内部错误')
        }else if(error.response.status===400){
          message.error('页面数据内容错误')
        }
      }
      return Promise.reject(error);
    }
  );
  export default request;